package org.example.common;

import java.sql.*;

import static org.example.LoginPage.currentUserName;

/**
 * 创建一个mysql类,进行登录前的用户名和密码的校验, 以及获取当前登录的用户id
 */

public class Mysql {
    private String username1;
    private String password1;
    private boolean flag = false;

    /**
     * 管理员登录的mysql验证
     *
     * @param username
     * @param password
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public boolean adminSearch(String username, String password) throws ClassNotFoundException, SQLException {
        this.username1 = username;
        this.password1 = password;
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM air_admin");

        while (resultSet.next()) {
            String username2 = resultSet.getString("admin_name");
            String password2 = resultSet.getString("password");
            if (username1.equals(username2) && password1.equals(password2)) {
                flag = true;
                break;
            }
        }

        // 关闭连接
        resultSet.close();
        statement.close();
        conn.close();

        if (flag == true) {
            return true;
        } else return false;
    }

    /**
     * 用户登录的mysql验证
     *
     * @param username
     * @param password
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public boolean userSearch(String username, String password) throws ClassNotFoundException, SQLException {
        this.username1 = username;
        this.password1 = password;
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM air_user");

        while (resultSet.next()) {
            String username2 = resultSet.getString("username");
            String password2 = resultSet.getString("password");
            if (username1.equals(username2) && password1.equals(password2)) {
                flag = true;
                break;
            }
        }

        // 关闭连接
        resultSet.close();
        statement.close();
        conn.close();

        if (flag == true) {
            return true;
        } else return false;
    }

    /**
     * 获取当前登录的用户id
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public int getCurrentUserId() throws ClassNotFoundException, SQLException { 
        int USERID = 0;
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
        Statement statement = conn.createStatement();
        String sql = "SELECT user_id FROM air_user WHERE username=" + "'" + currentUserName + "'";
        ResultSet resultSet1 = statement.executeQuery(sql);
        while (resultSet1.next()) {
            USERID = resultSet1.getInt("user_id");
        }

        // 关闭连接
        resultSet1.close();
        statement.close();
        conn.close();
        return USERID;
    }
}
